/*
 * Datamodel for admin 
 */
package models;

import helpers.Utils;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Data Model for Admin
 * @author luci
 */

public class Admin implements Serializable{

    public final static String TABLE_NAME = "admins";
    public final static String ID_FIELD_NAME = "id";
    private Integer id;
    public final static String NAME_FIELD_NAME = "name";
    private String name;
    public final static String LNAME_FIELD_NAME = "last_name";
    private String lastName;
    public final static String EMAIL_FIELD_NAME = "email";
    private String email;
    public final static String PASSWORD_FIELD_NAME = "password";
    private String password;
    public final static String GENDER_FIELD_NAME = "gender";
    private String gender;
    public final static String REGDATE_FIELD_NAME = "registered";
    private Date registrationDate;
    public final static String ACTIVE_FIELD_NAME = "active";
    private Integer active;
    public final static String PHOTO_FIELD_NAME = "photo";
    private String photo;

    public Admin() {
    }
    
    /**
     * private constructor because we only use it inside the class new
     * instances of this class are only created by the static methods
     */
    private Admin(Integer id, String name, String lastName, String email, String password, String gender, Date registrationDate, Integer active, String photo) {
        this.id = id;
        this.name = name;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
        this.gender = gender;
        this.registrationDate = registrationDate;
        this.active = active;
        this.photo = photo;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getRegistrationDate() {
        return registrationDate;
    }

    public void setRegistrationDate(Date registrationDate) {
        this.registrationDate = registrationDate;
    }

    public Integer getActive() {
        return active;
    }

    public void setActive(Integer active) {
        this.active = active;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public static Admin getAdminById(Integer id) throws SQLException {

        Connection conn = Utils.getConnection();
        
        String query = "SELECT * FROM " + TABLE_NAME + " WHERE "
                + "id=? ";
        PreparedStatement ps =
                conn.prepareStatement(query);
        ps.setInt(1, id);
        ResultSet rs = ps.executeQuery();
        if (!rs.next()) {
            return null;
        }
        Admin user = new Admin();
        user.setName(rs.getString(NAME_FIELD_NAME));
        user.setLastName(rs.getString(LNAME_FIELD_NAME));
        user.setEmail(rs.getString(EMAIL_FIELD_NAME));
        user.setPassword(rs.getString(PASSWORD_FIELD_NAME));
         user.setActive(rs.getInt(ACTIVE_FIELD_NAME));
        user.setPhoto(rs.getString(PHOTO_FIELD_NAME));
        rs.close();
        conn.close();
        return user;
    }   
}
